package com.xiaomili.halfsugar.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiaomili.halfsugar.R;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 发布界面
 */
public class PublishActivity extends AppCompatActivity {

    @InjectView(R.id.iv_cancle)
    ImageView ivCancle;
    @InjectView(R.id.tv_commit)
    TextView tvCommit;
    @InjectView(R.id.tv_see)
    TextView tvSee;
    @InjectView(R.id.tb_articl)
    Toolbar tbArticl;
    @InjectView(R.id.et_title)
    EditText etTitle;
    @InjectView(R.id.et_content)
    EditText etContent;

    private String title, content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);
        ButterKnife.inject(this);
    }


    @OnClick({R.id.iv_cancle, R.id.tv_commit, R.id.tv_see,R.id.et_title, R.id.et_content})
    public void onClick(View view) {
        title = etTitle.getText().toString().trim();
        content = etContent.getText().toString().trim();
        switch (view.getId()) {
            case R.id.iv_cancle:
                if (title.isEmpty() && content.isEmpty()) {
                    finish();
                } else if (title != null | content != null) {
                    String[] options = {"保存文章", "清空内容", "取消"};
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setItems(options, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case 0:
                                    startActivity(new Intent(PublishActivity.this, MainActivity.class));
                                    break;
                                case 1:
                                    etTitle.setText("");
                                    etContent.setText("");
//                                    startActivity(new Intent(PublishActivity.this, MainActivity.class));
                                    break;
                                case 2:
                                    return;
                            }
                        }
                    }).show();
                }
                break;
            case R.id.tv_commit:
                break;
            case R.id.tv_see:
                break;
            case R.id.et_title:
                if (content==null&&title==null){
                    tvCommit.setTextColor(getResources().getColor(R.color.colorTextPressed));
                }
                break;
            case R.id.et_content:
                if (content==null&&title==null){
                    tvCommit.setTextColor(getResources().getColor(R.color.colorTextPressed));
                }
                break;
        }
    }



    //跳转PublishActivity界面的方法
    public static void toPublishActivity(Context context) {
        Intent intent = new Intent(context, PublishActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.exit_anim,R.anim.exit_anim_out);
    }
}
